package com.lwl.Algorithmic_data_structure.class12二叉树;

import com.lwl.Utils.MyTreeNode;

/**
 * @author lwl
 * @Description 返回二叉树中任意两个节点距离中的最大值
 * @date 2023/6/1 8:27
 */
public class Code04getTreeMaxDistance {


    class Info {

        /**
         * 树高，即树的层级
         */
        int level;

        int maxDistance;

        public Info(int level, int maxDistance) {
            this.level = level;
            this.maxDistance = maxDistance;
        }
    }

    public Info getTreeMaxDistance(MyTreeNode treeNode) {
        if (treeNode == null) {
            return new Info(0, 0);
        }
        Info leftInfo = getTreeMaxDistance(treeNode.left);
        Info rightInfo = getTreeMaxDistance(treeNode.right);

        int curLevel = Math.max(leftInfo.level, rightInfo.level) + 1;
        int curMaxDistance = leftInfo.level + rightInfo.level + 1;
        int maxDistance = Math.max(Math.max(leftInfo.maxDistance, rightInfo.maxDistance), curMaxDistance);

        return new Info(curLevel, maxDistance);
    }

}
